<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 85%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="专业团队名称">
          <a-input placeholder="请输入团队名称" :disabled="teamDisabled"  v-decorator="[ 'teamName', validatorRules.teamName]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="团队角色">
          <a-input placeholder="请输入团队角色" :disabled="teamDisabled"  v-decorator="[ 'teamrole', validatorRules.teamrole]" />
        </a-form-item>
<!--        <a-form-item label="团队角色分配" :labelCol="labelCol" :wrapperCol="wrapperCol" >-->
<!--          <a-select-->
<!--            mode="multiple"-->
<!--            style="width: 100%"-->
<!--            placeholder="请选择团队角色"-->
<!--            v-model="selectedTeamRole">-->
<!--            <a-select-option v-for="(rolename,roleindex) in teamroleList" :key="roleindex.toString()" :value="rolename.id">-->
<!--              {{ rolename.teamRolename }}-->
<!--            </a-select-option>-->
<!--          </a-select>-->
<!--        </a-form-item>-->

        <!--团队成员-->
        <a-form-item label="团队成员" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-select
            mode="multiple"
            style="width: 100%"
            placeholder="请选择团队成员"
            v-model="selectedUser">
            <a-select-option v-for="(user,userindex) in userList" :key="userindex.toString()" :value="user.id">
              {{ user.realname}}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import {addTeam,editTeam,duplicateCheck } from '@/api/team'
  import {userall,roleNameAll,queryTeamRole,queryTeamUser} from '@/api/team'

  export default {
    name: "TeamModal",
    data () {
      return {
        title:"操作",
        visible: false,
        teamDisabled: false,
        model: {},
        userList:[],
        selectedUser:[],
        teamroleId: '',
        userId:'',
        teamroleList:[],
        selectedTeamRole:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          teamName:{
            rules: [
              { required: true, message: '请输入团队名称!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]},
          teamrole:{
            rules: [
              { required: true, message: '请输入团队角色!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]},
          username:{
            rules: [
              { required: true, message: '请输入团队成员!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]},

        },
      }
    },
    created () {
    },
    methods: {
      //初始化团队角色id表
      initialRoleNameList(){
        roleNameAll().then((res)=>{
          if(res.success){
            this.teamroleList = res.result;
            console.log("role:"+this.teamroleList)
          }else{
            console.log(res.message);
          }
        });
      },
      loadTeamRoles(teamid){
        queryTeamRole({teamid:teamid}).then((res)=>{
          if(res.success){
            this.selectedTeamRole = res.result;
          }else{
            console.log(res.message);
          }
        });
      },
      initialUserList(){
        userall().then((res)=>{
          if(res.success){
            this.userList = res.result;
            console.log("name:"+this.userList)
          }else{
            console.log(res.message);
          }
        });
      },
      loadTeamUsers(teamid){
        queryTeamUser({teamid:teamid}).then((res)=>{
          if(res.success){
            this.selectedUser = res.result;
          }else{
            console.log(res.message);
          }
        });
      },
      add () {
        this.edit({});
      },
      edit (record) {
        /*      let that = this;*/
        this.initialRoleNameList();
        this.initialUserList()
        this.form.resetFields();
        /*添加用户和角色列表*/
 /*       this.selectedUser = record.username;*/
        if(record.hasOwnProperty("id")){
          this.loadTeamUsers(record.id);
          this.picUrl = "Has no pic url yet";
        }
        if(record.hasOwnProperty("id")){
          this.loadTeamRoles(record.id);
          this.picUrl = "Has no pic url yet";
        }
     /*   this.selectedTeamRole=record.teamRolename;*/

        this.model = Object.assign({}, record);
        this.visible = true;

        //编辑页面禁止修改团队名称
        if(this.model.id){
          this.teamDisabled = true;
        }else{
          this.teamDisabled = false;
        }
        let that=this
        // that.model = Object.assign({}, this.model);
 /*      if(this.model.id){
          let b=[];
          if(this.model.username.indexOf(",")!=-1){
            b = this.model.username.split(",")
            if(b[b.length-1]==""){
              b=b.splice(0,b.length-1)
            }

            that.selectedUser = b
          }else {
            b = [this.model.username]
            that.selectedUser = b
          }

        }else {
          that.selectedUser=this.model.username
        }*/
        that.$nextTick(() => {
          that.form.setFieldsValue(pick(this.model,'teamName'))
        });

      },

      close () {
        this.$emit('close');
        this.visible = false;
        this.selectedTeamRole = [];
        this.selectedUser=[]
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let avatar = that.model.avatar;
            let formData = Object.assign(this.model, values);
            //formData.avatar = avatar;
           if(this.selectedUser){
             formData.selectedusernames = this.selectedUser.length>0?this.selectedUser.join(","):'';
            }else {
              formData.selectedusernames=''
            }
            console.log("woxiangkankan:                            "+formData.selectedusernames);
            if(this.userId!=''){
              formData.selectedusernames = this.userId;
            }

            formData.selectedrolenames = this.selectedTeamRole.length>0?this.selectedTeamRole.join(","):'';
            if(this.teamroleId!=''){
              formData.selectedrolenames = this.teamroleId;
            }



            let obj;
            console.log(formData)
            if(!this.model.id){
              obj=addTeam(formData);
            }else{
              obj=editTeam(formData);
            }
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      }

    }
  }
</script>

<style scoped>

</style>